package com.woniuxy.microcouponserver.handler;

import com.woniuxy.microcouponserver.util.DateUtil;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Date;

//@Component
public class StringToDateConverter implements Converter<String, Date> {
        /**
         * @param s  2020-09-12  |  2020-11-12 11:30:23
         * 解决方案：1、可以使用正则  2、判断字符串的长度
         */
        @Override
        public Date convert(String s) {
            Date date = null;
            try{
                if(StringUtils.hasLength(s)){
                    //获得长度
                    int lenth = s.length();
                    if(lenth == 10){
                        date = DateUtil.str2Date(s,"yyyy-MM-dd");
                    }else{
                        date = DateUtil.str2Date(s,"yyyy-MM-dd HH:mm:ss");
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
            return date;
        }
    }

